/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.proxy.component
{
	import com.string.StrUnit;
	
	/**
	 * 构造epo协议串 
	 * 
	 */		
	public class EPOParam extends POParam implements IPOParam
	{
		private var _eql:String;
		private var _dsName:String;
		private var _methods:String;
		private var _params: Array;
		private var _obj:Object;
		
		public var currentPage:int=0;
		public var pageSize:int=0;
		public var totalSize:int=0;
		private var fixTableArr:Array;
		
		/**
		 *	EPOParam		
		 *	构造方法：	EPOParam(eql:String=null)	
		 *	构造EQL语句请求串 格式如下：	
 		 *  <ds>
 		 *  <caller/>
 		 *  <appName></appName>
 		 *  <EPOParam>
 		 *  <dsName></dsName>
 		 *  <methods></methods>
 		 *  <eql></eql>
 		 *  <parameters/>
 		 *  <serviceParameters/>
 		 *  </EPOParam>
 		 *  </ds>
		 *  @param eql eqi语句
		 *  var eql:EPOParam=new EPOParam("SELECT * from table where userid=?");
		 *  var ft:FixTable=new FixTable();
		 *  ft.fixUser="CreaterId";
		 *  eql.addFixTable(ft);
		 *  eql.dsName="draftDoc"
		 *  eql.setString(1,"@userid");
		 * 
		 */		
		
		public function EPOParam(eql:String=null)
		{
			_params= new Array(); 
			fixTableArr=new Array();
			if (eql){
				this.EQL = eql;
			}
		}
		
		/**
		 * 服务名；对应toXML()中的<dsName></dsName> 
		 * @param _dsName
		 * 
		 */		
		public function set dsName(_dsName:String):void{this._dsName = _dsName;}
		public function get dsName():String{return this._dsName;}
		
		/**
		 * 操作方法;对应toXML()中的<methods></methods> 
		 * @param value
		 * 
		 */		
		public function set methods(value:String):void{_methods=value;}
		public function get methods():String{return _methods;}
		
		/**
		 * Eql语句 对应toXML()中的<eql></eql>  
		 * @param value
		 * 
		 */		
		public function set EQL(value:String):void{_eql = value;}
		public function get EQL():String{return _eql;}
		
		/**
		 * 添加字符类型的参数；
		 * @param index 为eql语句中参数的索引值
		 * @param paraValue 为实参 
		 * 
		 */		
		public function setString(index:int,paraValue:String):void
		{
			_obj=new Object();
			_obj["index"]=index;
			_obj["paraType"]="string";
			_obj["paraValue"]=paraValue;
			_params.push(_obj);			
		}
		
		/**
		 *  添加货币类型的参数 
		 * @param index  为eql语句中参数的索引值
		 * @param paraValue 为实参
		 * 
		 */		
		public function setCurrency(index:int,paraValue:String):void
		{
			_obj=new Object();
			_obj["index"]=index;
			_obj["paraType"]="currency";
			_obj["paraValue"]=paraValue;
			_params.push(_obj);			
		}
		
		/**
		 * 添加整型类型的参数  
		 * @param index 为eql语句中参数的索引值
		 * @param paraValue 为实参
		 * 
		 */		
		public function setInteger(index:int,paraValue:int):void
		{
			_obj=new Object();
			_obj["index"]=index;
			_obj["paraType"]="int";
			_obj["paraValue"]=paraValue;
			_params.push(_obj);			
		}
		
		/**
		 *  添加布尔类型的参数 
		 * @param index 为eql语句中参数的索引值
		 * @param paraValue 为实参
		 * 
		 */		
		public function setBoolean(index:int,paraValue:Boolean):void
		{
			_obj=new Object();
			_obj["index"]=index;
			_obj["paraType"]="boolean";
			_obj["paraValue"]=paraValue?"1":"0";
			_params.push(_obj);			
		}
		
		/**
		 *  添加大文本类型的参数 
		 * @param index 为eql语句中参数的索引值
		 * @param paraValue 为实参
		 * 
		 */		
		public function setText(index:int,paraValue:String):void
		{
			_obj=new Object();
			_obj["index"]=index;
			_obj["paraType"]="text";
			_obj["paraValue"]=paraValue;
			_params.push(_obj);			
		}
		
		/**
		 *   添加时间日期类型的参数 
		 * @param index 为eql语句中参数的索引值
		 * @param paraValue  为实参
		 * 
		 */		
		public function setDateTime(index:int,paraValue:String):void
		{
			_obj=new Object();
			_obj["index"]=index;
			_obj["paraType"]="date";
			_obj["paraValue"]=paraValue;
			_params.push(_obj);			
		}	
		
		/**
		 * 根据  fieldType 自动添加参数 
		 * @param index 为eql语句中参数的索引值
		 * @param paraValue 为实参
		 * @param fieldType 参数类型;包括：
		 * string 对应 setString
		 * currency，float 对应 setCurrency
		 * date，datetime 对应 setDateTime
		 * boolean，bit 对应 setBoolean
		 * int 对应 setInteger
		 * text 对应 setText
		 */		
		
		public function setParaValueByFieldType(index:int,paraValue:String,fieldType:String):void
		{
			switch(fieldType.toLowerCase())
			{
				case "string":{
					setString(index,paraValue);			
					break;
				}
				case "currency":
				case "float":{
					setCurrency(index,paraValue);
					break;
				}
				case "date":
				case "datetime":{
					setDateTime(index,paraValue);
					break;
				}
				case "boolean": 
				case "bit":{
					setBoolean(index,Boolean(paraValue));
					break;
				}
				case "int":{
					setInteger(index,int(paraValue));
					break;
				}
				case "text":{
					setText(index,paraValue);
					break;
				}	
			}
		}
		
		/**
		 * 返回所有参数集合 
		 * @return  Array
		 * 
		 */		
		public function get parameters():Array
		{
			return _params;
		}
		
		/**
		 * 添加  FixTable 对象。
		 * @param value  FixTable 对象
		 * 
		 */		
		public function addFixTable(value:FixTable):void
		{
			fixTableArr.push(value);		
		}
		
		
		/**
		 *  toXml():XML 组成xml协议串；格式如下	
		 *	<ds>
		 *	  <caller/>
		 *	  <appName></appName>
		 *	  <EPOParam>
		 *	    <dsName></dsName>
		 *	    <methods></methods>
		 *	    <eql></eql>
		 *	    <parameters/>
		 *	    <serviceParameters/>
		 *	  </EPOParam>
		 *	</ds>
		 *  @return 
		 * 
		 */		
		override  public function toXml():XML
		{
			var i:int;
			var pxml:XML=new XML(<EPOParam/>);
			if(currentPage!=0)
				pxml.appendChild(<currentPage>{currentPage.toString()}</currentPage>);
			if(pageSize!=0)
				pxml.appendChild(<pageSize>{pageSize.toString()}</pageSize>);	
			if(totalSize!=0)
				pxml.appendChild(<totalSize>{totalSize.toString()}</totalSize>);
					
			pxml.appendChild(<dsName>{dsName}</dsName>);
			pxml.appendChild(<transaction>{transaction}</transaction>);
			pxml.appendChild(<eql>{EQL}</eql>);
			pxml.appendChild(<parameters />);
			for(i=0;i<_params.length;i++)
			{
				pxml.parameters.appendChild(<param  index={_params[i]["index"]} type={_params[i]["paraType"]}>
					{cdata(_params[i]["paraValue"])}
					</param>
				);
			}
			
			var fixTables:XML=XML(<fixTables />);
			for(i=0;i<fixTableArr.length;i++)
			{
				fixTables.appendChild((fixTableArr[i] as FixTable).toXml());
			}
			pxml.appendChild(fixTables);
			
			return pxml;
		}
		
		private function cdata(value:String):XML
		{
			//value
			value=StrUnit.replace(value,"<![CDATA[","&lt;![CDATA[");
			value=StrUnit.replace(value,"]]>","]]&gt;");
			return  new XML("<![CDATA[" + value + "]]>");
		}
	}
}